package com.nursing.service;

import com.nursing.Dao.CheckinCheckoutDao;
import com.nursing.bean.CheckinCheckout;
import com.nursing.model.dto.CheckinCheckoutStatisticsDto;
import com.nursing.model.dto.ElderStatisticsDto;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckinCheckoutService {
    private final CheckinCheckoutDao checkinCheckoutDao;

    public CheckinCheckoutService(CheckinCheckoutDao checkinCheckoutDao) {
        this.checkinCheckoutDao = checkinCheckoutDao;
    }

    public Integer getCount() {
        return checkinCheckoutDao.getCount();
    }

    public List<CheckinCheckoutStatisticsDto> getMonthlyCount() {
        List<CheckinCheckoutStatisticsDto> dataList = checkinCheckoutDao.getMonthlyCount();
        Map<Integer, Integer> dataMap = new HashMap<>();

        // 先把数据库查询到的数据存入 Map
        for (CheckinCheckoutStatisticsDto dto : dataList) {
            dataMap.put(dto.getMonth(), dto.getCount());
        }

        // 生成 1-12 月的完整数据
        List<CheckinCheckoutStatisticsDto> fullDataList = new ArrayList<>();
        for (int month = 1; month <= 12; month++) {
            fullDataList.add(new CheckinCheckoutStatisticsDto(month, dataMap.getOrDefault(month, 0)));
        }

        return fullDataList;
    }

    public void insert(CheckinCheckout checkinCheckout) {
        checkinCheckoutDao.insert(checkinCheckout);
    }

    public void delete(Integer id) {
        checkinCheckoutDao.delete(id);
    }

    public List<CheckinCheckout> getByElderId(Integer elderId) {
        return checkinCheckoutDao.getByElderId(elderId);
    }


}
